package com.nus.fellowmark.modules.assignment.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.QuestionEntity;
import com.nus.fellowmark.modules.assignment.dto.DeleteByIdsDTO;
import com.nus.fellowmark.modules.assignment.dto.QuestionDTO;
import com.nus.fellowmark.modules.assignment.service.QuestionService;
import com.nus.fellowmark.modules.assignment.service.RubricService;
import com.nus.fellowmark.modules.assignment.vo.QuestionVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nus.fellowmark.common.utils.PageUtils;



/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10  
 */
@RestController
@RequestMapping("question")
@CrossOrigin
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private RubricService rubricService;

    @ApiOperation("list question")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = questionService.queryPage(params);

        return ServerResponseEntity.success("page", page);
    }


    @ApiOperation("select question by id ")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
		QuestionEntity question = questionService.getById(id);

        return ServerResponseEntity.success("question", question);
    }

    @ApiOperation("save question")
    @RequestMapping("/addQuestion")
    public ServerResponseEntity addQuestion(@RequestBody QuestionDTO questionDTO){
        QuestionVO questionVO = questionService.addQuestion(questionDTO);

        return ServerResponseEntity.success(questionVO);
    }

    @ApiOperation("update question")
    @RequestMapping("/updateQuestion")
    public ServerResponseEntity updateQuestion(@RequestBody QuestionDTO questionDTO){
		questionService.updateQuestion(questionDTO);

        return ServerResponseEntity.success();
    }

    @ApiOperation("delete question")
    @RequestMapping("/deleteQuestion")
    public ServerResponseEntity deleteQuestion(@RequestBody DeleteByIdsDTO deleteQuestionDTO){
		questionService.removeByIds(Arrays.asList(deleteQuestionDTO.getIds()));

        return ServerResponseEntity.success();
    }

    @ApiOperation("list question")
    @RequestMapping("/listQuestion")
    public ServerResponseEntity listQuestion(@RequestParam Integer assignmentId){
        List<QuestionVO> questions = questionService.listQuestions(assignmentId);

        return ServerResponseEntity.success(questions);
    }

}
